<template>
  <el-dialog
    v-model="dialogVisible"
    :title="editId ? '编辑菜单' : '新增菜单'"
    width="30%"
    :before-close="handleClose"
  >
    <el-form
      :model="adminForm"
      label-width="100px"
      ref="admin-Form"
      :rules="rules"
      class="demo-adminForm"
    >
      <el-form-item label="角色" prop="roleid">
        <el-select v-model="adminForm.roleid">
          <el-option :value="-1" label="请选择角色"></el-option>
          <el-option
            v-for="role in roles.rolesList"
            :key="role.id"
            :value="role.id"
            :label="role.rolename"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="账号" prop="username">
        <el-input
          :disabled="editId ? true : false"
          v-model="adminForm.username"
          placeholder="请输入账号"
          auto-complete="off"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          :disabled="editId ? true : false"
          v-model="adminForm.password"
          placeholder="请输入密码"
          auto-complete="off"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio v-model="adminForm.status" :label="1">启用</el-radio>
        <el-radio v-model="adminForm.status" :label="2">禁用</el-radio>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="closeDialog()">取消</el-button>
        <el-button type="primary" @click="submitAdmins()"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
// 导入store
import useRoleStore from "../../stores/roles";
import useAdminStore from "../../stores/admins";
const roles = useRoleStore();
const admins = useAdminStore();

onMounted(() => {
  // 调用函数,获取菜单数据
  roles.getRoleListActions();
});

// 定义控制弹窗显隐的变量
const dialogVisible = ref(false);

//关闭弹窗？
const handleClose = (done) => {
  ElMessageBox.confirm("确定关闭吗?", "关闭弹窗！", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      done();
      //   重置表单
      resetForm();
    })
    .catch(() => {
      // catch error
    });
};

// 关闭弹窗
const closeDialog = () => {
  resetForm();
};

// 定义菜单表单
let adminForm = reactive({
  roleid: -1,
  username: "",
  password: "",
  status: 1,
});

// 定义编辑id
let editId = ref("");

// 下拉框验证
const validRoleName = (rule, value, callback) => {
  if (value === -1) {
    callback(new Error("请选择角色"));
  } else {
    callback();
  }
};

// 表单规则
const rules = reactive({
  roleid: { validator: validRoleName, trigger: "blur" },
  username: { required: true, message: "请输入账号", trigger: "blur" },
  password: { required: true, message: "请输入密码", trigger: "blur" },
  status: { required: true, message: "请选择状态", trigger: "blur" },
});

// 清空表单，关闭弹窗
const resetForm = () => {
  // 重置编辑id
  editId = ref("");
  // 初始化表单
  adminForm = reactive({
    roleid: -1,
    username: "",
    password: "",
    status: 1,
  });
  // 关闭弹窗
  dialogVisible.value = false;
};

// 提交表单
const submitAdmins = () => {
  // console.log("roleForm", adminForm);
  if (editId.value) {
    adminForm.password = "";
    admins.editAdminAction({ ...adminForm, id: editId.value });
  } else {
    admins.addAdminAction(adminForm);
  }
  resetForm();
};

// 接收父组件数据
const acceptValue = (value) => {
  //   console.log("value", value);
  editId.value = value.id;
  adminForm = reactive({ ...value });
};

// 将变量与函数暴露出来,此代码片段放到下面
defineExpose({
  dialogVisible,
  acceptValue,
});
</script>

<style lang="css" scoped>
.el-select {
  width: 100%;
}
</style>
